package com.zzyl.Task;

import com.zzyl.entity.Reservation;
import com.zzyl.mapper.CostomerMapper;
import com.zzyl.vo.ReservationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;

/**
 * @author sjqn
 */
@Component
@Slf4j
public class MyTask {
    @Autowired
    private CostomerMapper costomerMapper;

    /**
     * 定时任务 每隔5秒触发一次
     */
    @Scheduled(cron = "0 1,31 * * * ?")
    public void executeTask() {
        List<ReservationVo> reservationVoList = costomerMapper.getAllAppoint();
        if (reservationVoList != null) {
            for (ReservationVo reservationVo : reservationVoList) {
                if (reservationVo.getStatus().equals(0) && reservationVo.getTime().isBefore(LocalDateTime.now())) {
                    reservationVo.setStatus(3);
                    Reservation reservation = new Reservation();
                    BeanUtils.copyProperties(reservationVo, reservation);
                    costomerMapper.updateStatus(reservation);
                }
            }
        }
    }
}